
package com.middlegroup.common.wrapper;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

/**
 * @author pc
 */
public class EncryptedResponseWrapper extends HttpServletResponseWrapper {
    private ByteArrayOutputStream cacheStream = new ByteArrayOutputStream();
    private PrintWriter bufferWriter;
    private ServletOutputStreamWrapper out;

    public EncryptedResponseWrapper(HttpServletResponse response) {
        super(response);
        this.bufferWriter = new PrintWriter(this.cacheStream);
        this.out = new ServletOutputStreamWrapper(this.cacheStream);
    }

    @Override
    public PrintWriter getWriter() throws IOException {
        return this.bufferWriter;
    }

    @Override
    public ServletOutputStream getOutputStream() throws IOException {
        super.getOutputStream();
        return this.out;
    }

    public String getResult() {
        try {
            return new String(this.cacheStream.toByteArray(), super.getCharacterEncoding());
        } catch (UnsupportedEncodingException var2) {
            throw new RuntimeException(var2);
        }
    }
}
